/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LXW
 * Date: 2022-03-10
 * Time: 10:36
 */
public class HashBucket {
    class Node {
        public int key;
        public int val;
        public Node next;
        public Node(int key,int val) {
            this.key = key;
            this.val = val;
        }
    }

    public Node[] array;
    public int size;
    public static double LOAD_FACTOR = 0.75;
    public HashBucket() {
        this.array = new Node[10];
    }
    public int put(int key,int val) {
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null) {
            if (cur.val == val) {
                int oldVal = cur.val;
                cur.val = val;
                return  oldVal;
            }
            cur = cur.next;
        }
        Node node = new Node(key,val);
        node.next = array[index];
        array[index] = node;
        size++;
        if (loadFactor(size) > LOAD_FACTOR) {
            resize();
        }
        return -1;
    }
    public double loadFactor(int size) {
        return size / array.length;
    }
    public void resize() {
        Node[] newArray = new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while (cur != null) {
                int newIndex = cur.key % newArray.length;
                Node curNext = cur.next;
                cur.next = newArray[newIndex];
                newArray[newIndex] = cur;
                cur = curNext;
            }
        }
        array = newArray;
    }
    public int get(int key) {
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null) {
            if (cur.key == key) {
                return cur.val;
            }
            cur = cur.next;
        }
        return -1;
    }
}
